package com.spdzen.service.impl;

import com.spdzen.model.SysDept;
import com.github.pagehelper.PageHelper;
import com.spdzen.mapper.SysDeptMapper;
import com.spdzen.service.SysDeptService;
import com.spdzen.support.PageNumParam;
import com.spdzen.support.PageOffsetParam;
import com.spdzen.support.PageUtils;
import com.spdzen.support.SimplePage;
import com.spdzen.support.SimpleServiceImpl;
import com.spdzen.support.SimpleStringUtils;
import com.spdzen.support.exception.ResultCodeException;
import com.spdzen.support.result.BizResultCode;
import com.spdzen.support.result.ResultCode;
import com.spdzen.vo.SysDeptAdminVO;
import com.spdzen.vo.search.SysDeptAdminSearch;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 组织机构  服务实现类
 */
@Service("sysDeptService")
public class SysDeptServiceImpl extends SimpleServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
	
	@Override
	public SimplePage<SysDeptAdminVO> selectPage(PageOffsetParam param, SysDeptAdminSearch search) {
		return selectPage(PageUtils.toPageNumParam(param), search);
	}
	
	@Override
	public SimplePage<SysDeptAdminVO> selectPage(PageNumParam param, SysDeptAdminSearch search) {
		checkSysDeptAdminSearch(search);
		
		int pageNum = PageUtils.checkPageNum(param.getPageNum());
		int pageSize = PageUtils.checkPageSize(param.getPageSize());
		
		PageHelper.startPage(pageNum, pageSize);
		
		List<SysDeptAdminVO> rows = baseMapper.selectAdminVO(search);
		
		return PageUtils.toSimplePage(rows);
	}
	
	@Override
	public SysDeptAdminVO selectOneAdminVO(Long id) {
		if(id == null || id.longValue() < 1){
			return null;
		}
		
		return baseMapper.selectOneAdminVO(id);
	}
	
	@Override
	public List<SysDeptAdminVO> selectAdminVO(SysDeptAdminSearch search) {
		checkSysDeptAdminSearch(search);
		return baseMapper.selectAdminVO(search);
	}
	
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public void inserIfPermission(SysDept entity) throws Exception {
		baseMapper.insert(entity);
		
		SysDept record = new SysDept();
		record.setCode(entity.getCode());
		if(selectCount(record) > 1) {
			throw new ResultCodeException("部门编号已存在！");
		}
	}
	
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public void updateIfPermission(SysDept entity) throws Exception {
		baseMapper.updateById(entity);
		
		SysDept record = new SysDept();
		record.setCode(entity.getCode());
		if(selectCount(record) > 1) {
			throw new ResultCodeException("部门编号已存在！");
		}
	}
	
	@Override
	public ResultCode deleteIfPermission(Long id) {
		SysDept record = new SysDept();
		record.setParentId(id);
		
		if(selectCount(record) > 0) {
			return BizResultCode.error("该部门有下级部门，不能删除！");
		}
		
		baseMapper.deleteById(id);
		
		return BizResultCode.success();
	}
	
	private void checkSysDeptAdminSearch(SysDeptAdminSearch search) {
		search.setCode(SimpleStringUtils.likeSqlString(search.getCode()));
		search.setName(SimpleStringUtils.likeSqlString(search.getName()));
	}
}
